/**
 * Created by GadflyBSD on 16/4/16.
 */
var meta = new Object();
var content = document.getElementsByTagName('meta');
var wait=60;
for(var i = 0; i < content.length; i++){
	if(content[i].getAttribute('name') == 'Authcode')
		meta.appurl = content[i].getAttribute('content');
	if(content[i].getAttribute('name') == 'appurl')
		meta.verifyImg = content[i].getAttribute('content');
	if(content[i].getAttribute('name') == 'upload')
		meta.upload = content[i].getAttribute('content');
	if(content[i].getAttribute('name') == 'angular')
		meta.angular = content[i].getAttribute('content');
	if(content[i].getAttribute('name') == 'appId')
		meta.appId = content[i].getAttribute('content');
	if(content[i].getAttribute('name') == 'timestamp')
		meta.timestamp = content[i].getAttribute('content');
	if(content[i].getAttribute('name') == 'nonceStr')
		meta.nonceStr = content[i].getAttribute('content');
	if(content[i].getAttribute('name') == 'signature')
		meta.signature = content[i].getAttribute('content');
}
wx.config({
	debug: false,
	appId: meta.appId,
	timestamp: meta.timestamp,
	nonceStr: meta.nonceStr,
	signature: meta.signature,
	jsApiList: ['chooseImage', 'previewImage', 'uploadImage', 'downloadImage']
});
function closeWindows(){
	$('#dialog_alert').hide();
	wx.ready(function(){
		wx.closeWindow();
	});
}
angular.module('myApp', ['myApp.controllers', 'myApp.services', 'myApp.directive']);
angular.module('myApp.services', [])
	.factory('myService', function($http){
		return {
			survey: function(type, data){
				"获得用户详细数据";
				return $http({
					url: meta.angular,
					method: 'post',
					data: angular.extend({ac: 'onePagePost', op: 'setSurvey', tp: type}, data)
				});
			}
		};
	});
angular.module('myApp.directive', [])
	.directive('uploadWidget', function(){
		"上传指令";
		return {
			restrict: 'A',
			template: '<ul class="weui_uploader_files"></ul><div class="weui_uploader_input_wrp"><input class="weui_uploader_input" type="button" /></div>',
			link: function(scope, elements, attrs){
				elements.bind('click', function(){
					wx.ready(function(){
						var count = (attrs.uploadWidget == 'survey_other')?9:1;
						wx.chooseImage({
							count: count, // 默认9
							sizeType: ['original', 'compressed'],   // 可以指定是原图还是压缩图，默认二者都有
							sourceType: ['album', 'camera'],        // 可以指定来源是相册还是相机，默认二者都有
							success: function (res) {
								var localIds = res.localIds;        // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
								elements.find('ul').prepend('<li class="weui_uploader_file"><img src="' + localIds + '" /></li>');
								wx.uploadImage({
									localId: localIds.toString(),
									isShowProgressTips: 1,
									success: function (resu) {
										var serverId = resu.serverId;
										elements.find('div').addClass('hidden');
										switch(attrs.uploadWidget){
											case 'survey_1':
												scope.surveyModel.survey_1 = serverId;
												break;
											case 'survey_2':
												scope.surveyModel.survey_2 = serverId;
												break;
											case 'survey_other':
												scope.surveyModel.survey_other = serverId;
												break;
										}
									}
								});
							}
						});
					});
				});
			}
		}
	});
angular.module('myApp.controllers', [])
	.controller('surveyCtrl', function($scope, myService){
		$scope.surveyModel = {
			survey: false,
			survey_1: 0,
			survey_2: 0,
			survey_other: 0
		};
		$scope.survey = function(type){
			$('#loading_toast').show();
			myService.survey(type, $scope.surveyModel).success(function(resp){
				$('#dialog_content').text(resp.content);
				$('#loading_toast').hide();
				$('#dialog_alert').show();
			});
		}
	})